package jsudoku;

import jsudoku.exception.InvalidSudokuException;

public class Sudoku {

    private int[][] sudoku;

    public Sudoku() {
        this.createSudoku();
    }

    
    public Sudoku(int[][] pSudoku) throws InvalidSudokuException{
        this.setSudoku(pSudoku);
    }

    public void addNumber(int pX, int pY, int pNumber){
        this.sudoku[pX][pY]=pNumber;
    }

    private void createSudoku() {
        this.sudoku = new int[9][9];
        int random = (int) (Math.random() * 20) + 5;
        for (int i = 0; i < random; i++) {
            int rand = 0;
            int rand2 = 0;
            do {
                rand = (int) (Math.random() * 9);
                rand2 = (int) (Math.random() * 9);
            } while (this.sudoku[rand][rand2] != 0);
            do {
                int randomNumber = (int) (Math.random() * 9)+1;
                this.sudoku[rand][rand2] = randomNumber;
            } while (!this.checkSudoku());
        }
    }

    public boolean checkSudoku() {
        for (int i = 0; i < 9; i++) {
        	/*BUG 3 START
            if(!this.checkColumn(i) || !this.checkLine(i) && !this.checkGroup(i)){
            BUG 3 END*/
        	if(!this.checkColumn(i) || !this.checkLine(i) || !this.checkGroup(i)){//BUG 3 RESOLUTION
                return false;
            }
        }
        return true;
    }

    public void emptyNumber(int pX, int pY){
        this.sudoku[pX][pY]=0;
    }

    public void emptySudoku(){
        this.sudoku= new int[9][9];
    }

    private boolean checkLine(int pLine) {
        int find;
        for (int i = 0; i < 9; i++) {
            find = 0;
            for (int j = 0; j < 9; j++) {
                if (this.sudoku[pLine][j] == (i + 1)) {
                    find++;
                }
            }
            if (find > 1) {
                return false;
            }
        }
        return true;
    }

    private boolean checkColumn(int pColumn) {
        int find;
        for (int i = 0; i < 9; i++) {
            find = 0;
            for (int j = 0; j < 9; j++) {
                if (this.sudoku[j][pColumn] == (i + 1)) {
                    find++;
                }
            }
            if (find > 1) {
                return false;
            }
        }
        return true;
    }

    private boolean checkGroup(int pGroup) {
        int[] coords = new int[2];
        switch (pGroup) {
            case 0:
                coords[0] = 0;
                coords[1] = 0;
                break;
            case 1:
                coords[0] = 0;
                coords[1] = 3;
                break;
            case 2:
                coords[0] = 0;
                coords[1] = 6;
                break;
            case 3:
                coords[0] = 3;
                coords[1] = 0;
                break;
            case 4:
                coords[0] = 3;
                coords[1] = 3;
                break;
            case 5:
                coords[0] = 3;
                coords[1] = 6;
                break;
            case 6:
                coords[0] = 6;
                coords[1] = 0;
                break;
            case 7:
                coords[0] = 6;
                coords[1] = 3;
                break;
            case 8:
                coords[0] = 6;
                coords[1] = 6;
                break;
        }
        int[][] group = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                group[i][j] = this.sudoku[(coords[0] + i)][(coords[1] + j)];
            }
        }
        int find = 0;
        for (int i = 0; i < 9; i++) {
            find = 0;
            for (int j = 0; j < 3; j++) {
                for (int x = 0; x < 3; x++) {
                    if (group[j][x] == (i+1)) {
                        find++;
                    }
                }
            }
            if (find > 1) {
                return false;
            }
        }
        return true;
    }

    public boolean isResolvedSudoku() {
        boolean retval = true;
        for (int i = 0; i < 9; i++) {
            retval = retval && this.isResolvedLine(i);
            retval = retval && this.isResolvedColumn(i);
            /* BUG  1 - missing function*/
            retval = retval && this.isResolvedGroup(i); //BUG 1 RESOLUTION
        }
        return retval;
    }

    private boolean isResolvedLine(int pLine) {
        int find = 0;
        for (int i = 0; i < 9; i++) {
        	find =0; //BUG 2 RESOLUTION
            for (int j = 0; j < 9; j++) {
                if (this.sudoku[pLine][j] == 0) {
                    return false;
                } else if (this.sudoku[pLine][j] == (i + 1)) {
                    find++;
                }
                /* BUG 2 RESOLUTION start*/
                if (find > 1) {
                    return false;
                }
                /* BUG 2 RESOLUTION end*/
            }
        }
        /* BUG 2 START 
        if (find == 9) {
            return true;
        }
        return false;
        BUG 2 END */
        return true; //BUG 2 RESOLUTION
    }

    private boolean isResolvedColumn(int pColumn) {
        int find = 0;
        for (int i = 0; i < 9; i++) {
        	find =0; //BUG 2 RESOLUTION
            for (int j = 0; j < 9; j++) {
                if (this.sudoku[j][pColumn] == 0) {
                    return false;
                }
                if (this.sudoku[j][pColumn] == (i + 1)) {
                    find++;
                }
                /* BUG 2 RESOLUTION start*/
                if (find > 1) {
                    return false;
                }
                /* BUG 2 RESOLUTION end*/ 
            }
        }
        /* BUG 2 START 
        if (find == 9) {
            return true;
        }
        return false;
        BUG 2 END */
        return true; //BUG 2 RESOLUTION
    }

    private boolean isResolvedGroup(int pGroup) {
        int[] coords = new int[2];
        switch (pGroup) {
            case 0: 
                coords[0] = 0;
                coords[1] = 0;
                break;
            case 1:
                coords[0] = 0;
                coords[1] = 3;
                break;
            case 2:
                coords[0] = 0;
                coords[1] = 6;
                break;
            case 3:
                coords[0] = 3;
                coords[1] = 0;
                break;
            case 4:
                coords[0] = 3;
                coords[1] = 3;
                break;
            case 5:
                coords[0] = 3;
                coords[1] = 6;
                break;
            case 6:
                coords[0] = 6;
                coords[1] = 0;
                break;
            case 7:
                coords[0] = 6;
                coords[1] = 3;
                break;
            case 8:
                coords[0] = 6;
                coords[1] = 6;
                break;
        }
        int[][] group = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                group[i][j] = this.sudoku[(coords[0] + i)][(coords[1] + j)];
            }
        }
        int find = 0;
        for (int i = 0; i < 9; i++) {
            find = 0;
            for (int j = 0; j < 3; j++) {
                for (int x = 0; x < 3; x++) {
                    if (group[j][x] == 0) {
                        return false;
                    } else if (group[j][x] == i) {
                        find++;
                    }
                    /* BUG 2 RESOLUTION start  */
                    if (find > 1) {
                        return false;
                    }
                    /* BUG 2 RESOLUTION end */
                }
            }
        }
        /* BUG 2 START 
        if (find == 9) {
            return true;
        }
        return false;
        BUG 2 END */
        return true; // BUG 2 RESOLUTION
    }

    private void setSudoku(int[][] pSudoku) throws InvalidSudokuException{
        this.sudoku= new int[9][9];
        if((pSudoku.length!=9)||(pSudoku[0].length!=9)){
            throw new InvalidSudokuException("Sudoku non valido: griglia di dimensioni diverse da 9x9");
        }
        for(int i=0; i<9; i++){
            for(int j=0; j<9; j++){
                if(pSudoku[i][j]<0 || pSudoku[i][j]>9){
                    throw new InvalidSudokuException("Sudoku non valido: I numeri al suo interno sono minori di 1 o maggiori di 9");
                }
            }
        }
        for(int i=0; i<9; i++){
            for(int j=0; j<9; j++){
                this.sudoku[i][j]=pSudoku[i][j];
            }
        }
    }


    public int[][] getSudoku() {
        return this.sudoku;
    }

}
